// SwcRight.cpp : implementation file
//

#include "stdafx.h"
#include "matrixcalibrator.h"
#include "SwcRight.h"
#include "ArrowDlg.h"
#include "DioControl.h"
#include "GRnR.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSwcRight

IMPLEMENT_DYNCREATE(CSwcRight, CFormView)

CSwcRight::CSwcRight()
	: CFormView(CSwcRight::IDD)
{
	//{{AFX_DATA_INIT(CSwcRight)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CSwcRight::~CSwcRight()
{
}

void CSwcRight::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSwcRight)
	DDX_Control(pDX, IDC_SWC_RIGHT, m_JazzedUpStackedWndCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSwcRight, CFormView)
	//{{AFX_MSG_MAP(CSwcRight)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSwcRight diagnostics

#ifdef _DEBUG
void CSwcRight::AssertValid() const
{
	CFormView::AssertValid();
}

void CSwcRight::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSwcRight message handlers

BOOL CSwcRight::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CFormView::PreCreateWindow(cs);
}

void CSwcRight::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	
}

BOOL CSwcRight::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

CWnd* CSwcRight::ContentWnd( int iIndex, CWnd* pParent )
{
	switch( iIndex )
	{
	case 0:
		{
			CRect		rEmpty;
			CListBox*	pContent = new CListBox;

			pContent->Create( WS_THICKFRAME | WS_VSCROLL | LBS_NOINTEGRALHEIGHT, rEmpty, pParent, iIndex + 1 );
			pContent->SetFont( GetFont() );
			return pContent;
		}
		break;
	case 1:
		{
			CGRnR* pdlgContent = new CGRnR;
			
			pdlgContent->Create(IDD_DLG_GRNR, pParent);
			
			return pdlgContent;
		}
		break;
	case 2:
		{
			CString		cs;
			int			iRand1, iRand2;
			CRect		rEmpty;
			CTreeCtrl*	pContent = new CTreeCtrl;
			
			pContent->Create( WS_THICKFRAME | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT, rEmpty, pParent, iIndex + 1 );
			pContent->SetFont( GetFont() );
			
			for( int i = 0; i < 10; i++ )
			{
				cs.Format( "Item %d", i + 1 );
				
				HTREEITEM hti1 = pContent->InsertItem( cs );
				
				iRand1 = 1 + ( rand() % 4 );
				
				for( int j = 0; j < iRand1; j++ )
				{
					cs.Format( "Item %d.%d", i + 1, j + 1 );
					
					HTREEITEM hti2 = pContent->InsertItem( cs, hti1 );
					
					if( rand() % 2 == 1 )
					{
						iRand2 = 1 + ( rand() % 4 );
						
						for( int k = 0; k < iRand2; k++ )
						{
							cs.Format( "Item %d.%d.%d", i + 1, j + 1, k + 1 );
							
							pContent->InsertItem( cs, hti2 );
						}
						
						pContent->Expand( hti2, TVE_EXPAND );
					}
				}
				
				pContent->Expand( hti1, TVE_EXPAND );
			}
			
			return pContent;
		}
		break;
	case 3:
		{
			CDioControl* pdlgContent = new CDioControl;
			
			pdlgContent->Create(IDD_DLG_DIO, pParent);
			
			return pdlgContent;
		}
		break;
	case 4:
		default:			// Default case creates a dialog too
		{
			CArrowDlg* pdlgContent = new CArrowDlg;
			
			pdlgContent->Create( IDD_DLG_ARROW, pParent );
			
			return pdlgContent;
		}
		break;
	}

	return (CWnd*)NULL;
}

void CSwcRight::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	ShowSizeGrip( TRUE );
	
	AddAnchor(IDC_SWC_RIGHT, TOP_CENTER, BOTTOM_RIGHT);
	
	CJazzUpTellTaleButton*	pbtJazzedUpRubric;
	CRect					rRect;
	CString					csRubric;
	UINT					nIconId = IDI_ICON1;
	UINT					nCtrlID = 0;
	
	rRect.SetRect( 10, 10, 10, 10 );		// CShadeButtonST crashes on an empty rect
	
	// "jazzed-up" control
	for(int i = 0; i < 5; i++)
	{
		switch( i )
		{
		case 0:
			csRubric	= "Debugging Console";
			nIconId		= IDI_ICON4;
			break;
		case 1:
			csRubric	= "GRnR Test";
			nIconId		= IDI_ICON1;
			break;
		case 2:
			csRubric	= "None";
			nIconId		= IDI_ICON5;
			break;
		case 3:
			csRubric	= "DIO Control";
			nIconId		= IDI_ICON2;
			break;
		case 4:
			csRubric	= "Control Calibration";
			nIconId		= IDI_ICON3;
			break;
		default:
			csRubric = "Default?";
		}
		
		pbtJazzedUpRubric = new CJazzUpTellTaleButton;
		
		pbtJazzedUpRubric->Create(csRubric, WS_CHILD, rRect, &m_JazzedUpStackedWndCtrl, nCtrlID++);
		pbtJazzedUpRubric->SetFont(GetFont());
		pbtJazzedUpRubric->SetIcon(nIconId);
		
		m_JazzedUpStackedWndCtrl.AddPane(pbtJazzedUpRubric, ContentWnd(i, &m_JazzedUpStackedWndCtrl));
	}
}

void CSwcRight::AddAnchor(HWND wnd, CSize tl_type, CSize br_type)
{
	ASSERT(wnd != NULL && ::IsWindow(wnd));
	ASSERT(::IsChild(*this, wnd));
	ASSERT(tl_type != NOANCHOR);
	
	// get control's window class
	
	CString st;
	GetClassName(wnd, st.GetBufferSetLength(MAX_PATH), MAX_PATH);
	st.ReleaseBuffer();
	st.MakeUpper();
	
	// add the style 'clipsiblings' to a GroupBox
	// to avoid unnecessary repainting of controls inside
	if (st == "BUTTON")
	{
		DWORD style = GetWindowLong(wnd, GWL_STYLE);
		if (style & BS_GROUPBOX)
			SetWindowLong(wnd, GWL_STYLE, style | WS_CLIPSIBLINGS);
	}
	
	// wnd classes that don't redraw client area correctly
	// when the hor scroll pos changes due to a resizing
	BOOL hscroll = FALSE;
	if (st == "LISTBOX")
		hscroll = TRUE;
	
	// wnd classes that need refresh when resized
	BOOL refresh = FALSE;
	if (st == "STATIC")
	{
		DWORD style = GetWindowLong(wnd, GWL_STYLE);
		
		switch (style & SS_TYPEMASK)
		{
		case SS_LEFT:
		case SS_CENTER:
		case SS_RIGHT:
			// word-wrapped text needs refresh
			refresh = TRUE;
		}
		
		// centered images or text need refresh
		if (style & SS_CENTERIMAGE)
			refresh = TRUE;
		
		// simple text never needs refresh
		if (style & SS_TYPEMASK == SS_SIMPLE)
			refresh = FALSE;
	}
	
	// get dialog's and control's rect
	CRect wndrc, objrc;
	
	GetClientRect(&wndrc);
	::GetWindowRect(wnd, &objrc);
	ScreenToClient(&objrc);
	
	CSize tl_margin, br_margin;
	
	if (br_type == NOANCHOR)
		br_type = tl_type;
	
	// calculate margin for the top-left corner
	
	tl_margin.cx = objrc.left - wndrc.Width() * tl_type.cx / 100;
	tl_margin.cy = objrc.top - wndrc.Height() * tl_type.cy / 100;
	
	// calculate margin for the bottom-right corner
	
	br_margin.cx = objrc.right - wndrc.Width() * br_type.cx / 100;
	br_margin.cy = objrc.bottom - wndrc.Height() * br_type.cy / 100;
	
	// add to the list
	Layout obj(wnd, tl_type, tl_margin,	br_type, br_margin, hscroll, refresh);
	m_arrLayout.Add(obj);
}

void CSwcRight::ShowSizeGrip(BOOL bShow)
{
	if (m_bShowGrip != bShow)
	{
		m_bShowGrip = bShow;
		InvalidateRect(&m_rcGripRect);
	}
}

CStackedWndCtrl* CSwcRight::GetStackedWnd()
{
	return &m_JazzedUpStackedWndCtrl;
}
